package com.gxa.modules.pc.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gxa.common.exception.ResultException;
import com.gxa.common.utils.DateUtils;
import com.gxa.common.utils.Query;
import com.gxa.common.utils.TPage;
import com.gxa.modules.app.hospitalization.persional.service.PatientService;
import com.gxa.modules.model.entity.book.BookHsCheck;
import com.gxa.modules.model.entity.book.BookHsTimeNum;
import com.gxa.modules.model.entity.fee.Patient;
import com.gxa.modules.model.vo.BookHsDetail;
import com.gxa.modules.model.vo.DayAndWeekDayVO;
import com.gxa.modules.model.vo.TimeAndNumVO;
import com.gxa.modules.pc.order.mapper.BookHsCheckMapper;
import com.gxa.modules.pc.order.mapper.BookHsTimeNumMapper;
import com.gxa.modules.pc.order.service.BookHsCheckService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 预约核酸检测表 服务实现类
 * </p>
 *
 * @author zhuiyi
 * @since 2022-11-12 09:47:43
 */
@Slf4j
@Service
public class BookHsCheckServiceImpl extends ServiceImpl<BookHsCheckMapper, BookHsCheck> implements BookHsCheckService {
    @Autowired
    private BookHsTimeNumMapper bookHsTimeNumMapper;

    @Autowired
    private PatientService patientService;

    @Autowired
    private BookHsCheckMapper bookHsCheckMapper;
    @Override
    public TPage queryByPage(Map<String, Object> searchMap) {
        //其他的查询参数
        String departmentId = (String) searchMap.get("departmentId");
        String orderField = (String) searchMap.get("orderField");
        String order = (String) searchMap.get("order");
        log.info("------username----{}--id---{}--", departmentId, departmentId);
        IPage<BookHsCheck> oldPage = this.page(new Query<BookHsCheck>().getPage(searchMap), new QueryWrapper<BookHsCheck>()
                .eq(!StringUtils.isBlank(departmentId), "id", departmentId)
        );
        return new TPage(oldPage);
    }

    @Override
    public List<DayAndWeekDayVO> getWeek7() {
        List<String> dayAndAfterNumDays = DateUtils.getDayAndAfterNumDays(DateUtils.getStringToday(), 7);
        List<DayAndWeekDayVO> week7 = new ArrayList<>();
        DayAndWeekDayVO dayAndWeekDay = null;
        for(int i=0;i<dayAndAfterNumDays.size();i++){
            dayAndWeekDay = new DayAndWeekDayVO();
            String dayStr = dayAndAfterNumDays.get(i);
            dayAndWeekDay.setDay(dayStr);
            dayAndWeekDay.setWeekDay(DateUtils.getDayWithWeekString(DateUtils.stringToDate(dayStr,"yyyy-MM-dd")));
            week7.add(dayAndWeekDay);
        }
        return week7;
    }
    @Override
    public Map<String, Object> getWeek7plus() {
        List<String> dayAndAfterNumDays = DateUtils.getDayAndAfterNumDays(DateUtils.getStringToday(), 7);
        List<DayAndWeekDayVO> week7 = new ArrayList<>();
        DayAndWeekDayVO dayAndWeekDay = null;
        for(int i=0;i<dayAndAfterNumDays.size();i++){
            dayAndWeekDay = new DayAndWeekDayVO();
            String dayStr = dayAndAfterNumDays.get(i);
            dayAndWeekDay.setStrDay(dayStr);
            dayAndWeekDay.setDay(dayStr.split("\\-")[2]);
            dayAndWeekDay.setWeekDay(DateUtils.getDayWithWeekString(DateUtils.stringToDate(dayStr,"yyyy-MM-dd")));
            week7.add(dayAndWeekDay);
        }
        Map map = new HashMap();
        map.put("today",DateUtils.getStringToday().split("\\-")[2]);
        map.put("strToday",DateUtils.getStringToday());
        map.put("week7",week7);
        return map;
    }

    public Map<String,Object> getTimeAndNum(String day){

        BookHsTimeNum dto = this.bookHsTimeNumMapper.selectOne(new QueryWrapper<BookHsTimeNum>().eq("day", day));
        Map<String,Object> map = new HashMap<>();
        List<TimeAndNumVO> am = new ArrayList<>();
        List<TimeAndNumVO> pm = new ArrayList<>();
        try {
            am.add(new TimeAndNumVO("9:00", dto.getA9()));
            am.add(new TimeAndNumVO("9:30", dto.getA9H()));
            am.add(new TimeAndNumVO("10:00", dto.getA10()));
            am.add(new TimeAndNumVO("10:30", dto.getA10H()));
            am.add(new TimeAndNumVO("11:00", dto.getA11()));
            am.add(new TimeAndNumVO("11:30", dto.getA11H()));

            pm.add(new TimeAndNumVO("14:00", dto.getP14()));
            pm.add(new TimeAndNumVO("14:30", dto.getP14H()));
            pm.add(new TimeAndNumVO("15:00", dto.getP15()));
            pm.add(new TimeAndNumVO("14:30", dto.getP15H()));
            pm.add(new TimeAndNumVO("16:00", dto.getP16()));
            pm.add(new TimeAndNumVO("16:30", dto.getP16H()));
        }catch (NullPointerException e){
            throw new ResultException("请换个医生或 时间");
        }
        map.put("am",am);
        map.put("pm",pm);

        return map;
    }

    @Override
    public Map<String,Object> getDetail(Integer id, String time) {
        List<Patient> userDetail = patientService.query(id);
        String youknow = "预约须知：\n" +
                "\n" +
                "1.核酸检测结果有效期：7天。\n" +
                "\n" +
                "2.受检者必须全程佩戴口罩，采样时将口罩封装在塑料袋里放入口袋中，采样后立即佩戴好口罩。\n" +
                "\n" +
                "3、新冠病毒核酸检测（咽拭子）采样前2小时不进食、不吸烟、不饮酒，采样前清水漱口。\n" +
                "\n" +
                "4、预约成功后如须退款，请于我院退费窗口办理。";

        Map<String,Object> map = new HashMap<>();
        BookHsDetail bookHsDetail = new BookHsDetail();
        bookHsDetail.setName("新型冠状病毒核酸检测（咽拭子）");
        bookHsDetail.setNum(1);
        bookHsDetail.setPrice(3);
        bookHsDetail.setYouknow(youknow);

        map.put("user",userDetail);
        map.put("other",bookHsDetail);

        //往 预约表里加入
        BookHsCheck bookHsCheck = new BookHsCheck();
        bookHsCheck.setAppointTime(new Date());
        bookHsCheck.setUserId(Long.parseLong(Integer.toString(id)));
        bookHsCheck.setWaitCheckTime(time);
        bookHsCheck.setDepartment("保健科");
        bookHsCheck.setCardNum(userDetail.get(0).getCardNum());
        bookHsCheck.setPhone(userDetail.get(0).getPhone());
        //idcard
        bookHsCheckMapper.insert(bookHsCheck);

        return map;
    }

    @Override
    public List<BookHsCheck> getDetailbyId(Integer id) {
        return bookHsCheckMapper.selectList(new QueryWrapper<BookHsCheck>().eq("user_id",id));

    }


}
